package com.fc.v2.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;

public class HttpSpiderUtils {

    private static List<String> userAgents = new ArrayList<>();

    static {
        userAgents.add("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36");
        userAgents.add("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1");
        userAgents.add("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36");
        userAgents.add("Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50");
        userAgents.add("Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11");
        userAgents.add("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)");
        userAgents.add("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)");
        userAgents.add("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)");
        userAgents.add("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)");
        userAgents.add("Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;");
    }

    /***
     * 随机一个请求头
     * @return
     */
    public static String getUserAgent() {
        int index = new Random().nextInt(10); // 随机获取一个0-9的整数作为索引
        return userAgents.get(index);
    }

    /**
     * 发送post请求
     * @return
     */
    public static JSONObject post(Map<String, String> params, String url) {
        int times = 0;
        while (times < 10) {
            HttpClient client = new HttpClient();
            PostMethod postMethod = new PostMethod(url);

            //添加请求头
            postMethod.addRequestHeader("User-Agent", getUserAgent());
            postMethod.addRequestHeader("content-type", "application/json, text/javascript, */*; q=0.01");

            //添加请求参数
            Set<String> keys = params.keySet();
            for (String key: keys) {
                String value = params.get(key);
                postMethod.addParameter(key, value);
            }
            try {
                client.executeMethod(postMethod);
                String res = postMethod.getResponseBodyAsString();
                JSONObject dataRes = JSONObject.parseObject(res);
                System.out.println(dataRes);
                return dataRes;
            } catch (IOException e) {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                times = times  + 1;
                e.printStackTrace();
            }finally {
                postMethod.releaseConnection();
            }
        }
        return null;
    }

    public static void main(String[] args) {
        String page = "1";
        String limit = "10";
        String jsonStr = "{\"projectName\":\"\",\"certNo\":\"\",\"developer\":\"\"}";
        Map params = new HashMap<String, String>();
        params.put("page", page);
        params.put("limit", limit);
        params.put("jsonStr", jsonStr);
        String url  = "https://yc.nxzhzj.cn/estateTradeR01/portals/query!listSalePermitProject";
        post(params, url);
    }
}
